package com.yanqu.road.server.cmd.yrank.union;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.rank.yrank.UnionYRank;
import com.yanqu.road.entity.rank.yrank.UserYRank;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUnion;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUser;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.AutoSendRewardProto;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.pb.crossunion.activity.CrossUnionActivityRankDataProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.rank.YRankProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.biaoju.CrossBiaoJuActivity;
import com.yanqu.road.server.manager.activity.biaoju.CrossBiaoJuMgr;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.server.manager.yrank.CrossYRankMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Cmd(code = CrossProtocol.C2_CROSS_YRANK_AUTO_SEND_UNION_RANK, desc = "")
public class CrossYRankAutoSendUnionRankCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        YRankProto.YRankReqMsg reqMsg = YRankProto.YRankReqMsg.parseFrom(packet.getBytes());
        int conditionType = Integer.valueOf(reqMsg.getIdKey());

        if (conditionType == eGamePlayerEventType.BiaoJuUnionExpRank.getValue()) {
            CrossBiaoJuActivity activity = CrossBiaoJuMgr.getActivity(reqMsg.getActivityId(), reqMsg.getGroupId());
            if (activity != null) {
                activity.autoSendUnionRank(serverId);
            }
            return;
        }

        List<UnionYRank> rankList = CrossYRankMgr.getSortUnionRankList(reqMsg.getActivityId(), reqMsg.getGroupId(), reqMsg.getIdKey());

        AutoSendRewardProto.CrossUnionRankMsg.Builder builder = AutoSendRewardProto.CrossUnionRankMsg.newBuilder();
        builder.setActivityId(reqMsg.getActivityId());
        int rank = 1;
        for (UnionYRank unionYRank : rankList) {
            // 成员 && 职位
            List<CrossUnionActivityRankDataProto.CrossUnionMemberPosDataMsg> posList = new ArrayList<>();
            UnionActivityGroupUnion unionData = Cross2UnionActivityGroupMgr.getUnionData(activityId, unionYRank.getUnionUid());
            if (unionData != null) {
                ConcurrentHashMap<Long, UnionActivityGroupUser> userMap = unionData.getUserMap();
                for (UnionActivityGroupUser groupUser : userMap.values()) {
                    posList.add(CrossUnionActivityRankDataProto.CrossUnionMemberPosDataMsg.newBuilder()
                            .setUserId(groupUser.getUserId())
                            .setPos(groupUser.getPosition())
                            .build());
                }
            }else{
                for (Map.Entry<Long, Integer> entry : unionYRank.getPosMap().entrySet()) {
                    posList.add(CrossUnionActivityRankDataProto.CrossUnionMemberPosDataMsg.newBuilder()
                            .setUserId(entry.getKey())
                            .setPos(entry.getValue())
                            .build());
                }
            }

            // 商会榜
            builder.addData(AutoSendRewardProto.UnionRankMsg.newBuilder()
                    .setUnionId(unionYRank.getUnionUid())
                    .setRank(rank++)
                    .setConditionType(conditionType)
                    .addAllPosList(posList)
            );
        }

        YanQuMessage message = YanQuMessageUtils.buildMessage(GameProtocol.S_CROSS_ACTIVITY_AUTO_SEND_UNION_REWARD, builder);
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), message);
    }

    @Override
    public void sendData() {

    }
}
